<%@ page
	import="com.serviceprovider.business.utility.SecurityContextUser"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%--@elvariable id="states" type="java.util.List<State>"--%>
<%--@elvariable id="countries" type="java.util.List<Country>"--%>
<%--@elvariable id="roleList" type="java.util.List<Role>"--%>
<%--@elvariable id="elements" type="com.serviceprovider.business.module.user.model.interfaces.IElements"--%>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<%@include file="/WEB-INF/taglibs/common/taglibs.jsp"%>
<%@page import="com.serviceprovider.business.module.common.Page"%>
<%@page
	import="com.serviceprovider.business.module.user.model.forms.ViewUserForm"%>

<head>
<title><spring:message code="${elements.suffix}.view.user.title" /></title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" href="<c:url value="/css/styles.css"/>" />
<script src="<c:url value="/javascripts/common.js"/>"
	type="text/javascript"></script>
<style>
.pager-page {
	text-decoration: underline;
	color: blue;
	cursor: pointer;
}
</style>
</head>

<body>
<c:if test="<%=SecurityContextUser.isAdmin()%>">
	<%@include file="/WEB-INF/jsp/admin/top_menu.jsp"%>
</c:if>
<c:if test="<%=SecurityContextUser.isServiceProvider()%>">
	<%@include file="/WEB-INF/jsp/serviceprovider/top_menu.jsp"%>
</c:if>
<c:if test="<%=SecurityContextUser.isPayee()%>">
	<%@include file="/WEB-INF/jsp/serviceprovider/top_menu.jsp"%>
</c:if>


<table width=100%>
	<tr>
		<c:if test="<%=SecurityContextUser.isAdmin()%>">
			<td><c:import url="/WEB-INF/jsp/menu/admin_users_menu.jsp" /><br />
			</td>
		</c:if>
		<c:if test="<%=SecurityContextUser.isServiceProvider()%>">
			<td><c:import url="/WEB-INF/jsp/menu/sp_users_menu.jsp" /><br />
			</td>
		</c:if>
	</tr>

	<tr>
		<td><%@include file="/WEB-INF/jsp/include/messages.jsp"%>
		<br />
		</td>
	</tr>
</table>

<form:form id="searchForm" action="search_users.htm"
	commandName="viewUserForm" method="post">
	<form:hidden id="sortColumnId" path="sortColumn" />
	<form:hidden id="sortOrderId" path="sortOrder" />
	<form:hidden path="inGuiTotal" />
	<form:hidden path="level1UserId" />
	<form:hidden path="totalRecords" />
	<form:hidden id="pageNoId" path="pageNumber" />
	<form:hidden path="pagesAvailable" />
	<form:hidden path="newSearch" />
	<form:hidden path="recordSize" id="recordSize" />

	<table width=98% class="formTable">
		<tr>
			<td><spring:message
				code="${elements.suffix}.view.user.firstname" /></td>
			<td><form:input path="firstName" size="40" id="firstNameId" /></td>
			<td><spring:message code="${elements.suffix}.view.user.lastname" /></td>
			<td><form:input path="lastName" size="40" id="lastNameId" /></td>
			<td>
			<div id="roleFilterComboLabelId"
				style="display: ${elements.roleFilterComboLabel}"><spring:message
				code="${elements.suffix}.view.user.select.role" /></div>
			</td>
			<td>
			<div id="roleFilterComboId"
				style="display: ${elements.roleFilterCombo}"><form:select
				path="roleId" id="roleId">
				<form:option value="0">
					<spring:message code="${elements.suffix}.view.user.select.all" />
				</form:option>
				<c:forEach items="${roleList}" var="item" varStatus="i">
					<form:option value="${item.id}">${item.name}</form:option>
				</c:forEach>
			</form:select></div>
			</td>
		</tr>
		<tr>
			<td><spring:message code="${elements.suffix}.view.user.email" /></td>
			<td><form:input path="email" size="40" id="emailId" />
			<div class="errors"><form:errors path="email" /></div>
			</td>
			<td><spring:message code="${elements.suffix}.view.user.efin" /></td>
			<td><form:input path="efin" size="40" id="efinId" />
			<div class="errors"><form:errors path="efin" /></div>
			</td>
			<td><spring:message code="${elements.suffix}.view.user.ptin" /></td>
			<td><form:input path="ptin" size="40" id="ptinId" />
			<div class="errors"><form:errors path="ptin" /></div>
			</td>
		</tr>
		<tr>
			<td><spring:message code="${elements.suffix}.view.user.ssn" /></td>
			<td><form:input path="ssn" size="40" id="ssnId" />
			<div class="errors"><form:errors path="ssn" /></div>
			</td>
			<td><spring:message
				code="${elements.suffix}.view.user.businessname" /></td>
			<td><form:input path="businessName" size="40"
				id="businessNameId" />
			<div class="errors"><form:errors path="businessName" /></div>
			</td>
			<td><spring:message
				code="${elements.suffix}.view.user.refNumber" /></td>
			<td><form:input path="refNumber" size="40"
				id="refNumber" />
			<div class="errors"><form:errors path="refNumber" /></div>
			</td>
		</tr>
		<tr>
			<td><spring:message code="${elements.suffix}.view.user.country" /></td>
			<td><form:select path="country.code" id="countryId">
				<form:option value="" label="" />
				<form:options items="${countries}" itemValue="code" itemLabel="name" />
			</form:select>
			<div class="errors"><form:errors path="country" /></div>
			</td>
			<td><spring:message code="${elements.suffix}.view.user.city" /></td>
			<td><form:input path="city" size="40" id="cityId" />
			<div class="errors"><form:errors path="city" /></div>
			</td>
			<td><spring:message code="${elements.suffix}.view.user.zip" /></td>
			<td><form:input path="zip" size="40" id="zipId" />
			<div class="errors"><form:errors path="zip" /></div>
			</td>

		</tr>
	</table>
	<table align="center">
		<tr>
			<td align="right"><input type="button" onclick="submitForm()"
				class='ui-button ui-state-default ui-corner-all'
				value="<spring:message code="${elements.suffix}.view.user.search"/>" />
			</td>
			<td><input type="button" onclick="clearFilters(); submitForm()"
				class='ui-button ui-state-default ui-corner-all'
				value="<spring:message code="${elements.suffix}.view.user.clear"/>" />
			</td>
		</tr>
	</table>
</form:form>

<form:form commandName="viewUserForm" action="view_users.htm"
	method="post">
	<form:hidden path="sortColumn" />
	<form:hidden path="sortOrder" />
	<form:hidden path="inGuiTotal" />
	<form:hidden path="level1UserId" />
	<form:hidden path="totalRecords" />
	<form:hidden path="pageNumber" />
	<form:hidden path="pagesAvailable" />
	<form:hidden path="newSearch" />


	<table width=98% class="formTable" cellspacing="0">

		<tr class="ui-state-default ui-th-column ui-th-ltr">
			<c:if test="${viewUserForm.pageNumber > 1}">
				<td align="left" colspan="10"><a onclick="previous()" href="#"
					class="mouseEffect"><span class="ui-icon ui-icon-seek-prev"
					style="float: left;"></span><spring:message
					code="${elements.suffix}.view.user.previous" /></a></td>
			</c:if>
			<c:if test="${viewUserForm.pageNumber < viewUserForm.pagesAvailable}">
				<td colspan="10" align="right"><a onclick="next()" href="#"
					class="mouseEffect"><span class="ui-icon ui-icon-seek-next"
					style="float: right;"></span><spring:message
					code="${elements.suffix}.view.user.next" /></a></td>
			</c:if>
		</tr>
	</table>
	<table width=98% class="formTable" cellspacing="0">
		<tr class="formHeader">
			<td colspan="8"><spring:message
				code="${elements.suffix}.view.user.userlist" /></td>
		</tr>
		<tr>
			<td width="3%"></td>
			<td width="17%"><a href="#"><span class="headerLink"></span><b
				onclick="sort('email')"><spring:message
				code="${elements.suffix}.view.user.email" /></b></a></td>
			<td width="17%"><a href="#"><span class="headerLink"></span><b
				onclick="sort('firstName')"><spring:message
				code="${elements.suffix}.view.user.firstname" /></b></a></td>
			<td width="17%"><a href="#"><span class="headerLink"></span><b
				onclick="sort('lastName')"><spring:message
				code="${elements.suffix}.view.user.lastname" /></b></a></td>
				
			<c:if test="<%=SecurityContextUser.isAdmin()%>">
				<td width="17%"><a><span class="headerLink"></span><b><spring:message
					code="${elements.suffix}.view.user.level1user" /></b></a></td>
				<td width="17%"><a href="#"><span class="headerLink"></span><b
					onclick="sort('role')"> <spring:message
					code="${elements.suffix}.view.user.role" /></b></a></td>
			</c:if>
			<td width="13%"><spring:message
				code="${elements.suffix}.view.user.transaction" /></td>
			<td width="13%"><spring:message
				code="${elements.suffix}.view.user.payees" /></td>
			<td width="13%"><spring:message
				code="${elements.suffix}.view.user.orders" /></td>
			<td width="5%" align="center"><b><spring:message
				code="${elements.suffix}.view.user.active" /></b></td>
			<td width="5%" align="center"><b><spring:message
				code="${elements.suffix}.view.user.ingui" /></b></td>
			<td width="5%" align="center"><b><spring:message
				code="${elements.suffix}.view.user.delete" /></b></td>
		</tr>
		<c:forEach items="${viewUserForm.usersList}" var="user" varStatus="i">
			<form:hidden path="usersList[${i.index}].id" />
			<input type="hidden" name="usersId" value="${user.id}" />
			<c:if test="${user.deleted}">
				<tr bgcolor="lightgray">
			</c:if>
			<c:if test="${!user.deleted}">
				<tr>
			</c:if>
			<td bgcolor="white" align="center"><a
				href="../users/edit_user_details.htm?id=${user.id}"> <img
				src="../images/editicon.png" border="0" alt="edit" height="20"
				width="20" /></a></td>
			<form:hidden path="usersList[${i.index}].email" />
			<td><a href="../users/view_user_details.htm?id=${user.id}">${user.email}</a></td>
			<td>${user.firstName} <form:hidden
				path="usersList[${i.index}].firstName" /></td>
			<td>${user.lastName} <form:hidden
				path="usersList[${i.index}].lastName" /></td>
			<c:if test="<%=SecurityContextUser.isAdmin()%>">
				<td><a
					href="../users/view_user_details.htm?id=${user.level1User.id}">${user.level1User.email}</a>
				<form:hidden path="usersList[${i.index}].level1User.email" /></td>
				<td>${user.role.name} <form:hidden
					path="usersList[${i.index}].role.name" /></td>
			</c:if>
			<td><a href=""><spring:message
				code="${elements.suffix}.view.user.transaction" /></a></td>
			<td><a href=""><spring:message
				code="${elements.suffix}.view.user.orders" /></a></td>
			<td><a href=""><spring:message
				code="${elements.suffix}.view.user.payees" /></a></td>
			<td align="center"><form:hidden
				path="usersList[${i.index}].enabled" /> <form:hidden
				path="usersList[${i.index}].inGui" /> <form:hidden
				path="usersList[${i.index}].deleted" /> <input type="checkbox"
				name="usersListActivated" value="${user.id}"
				<c:if test="${user.enabled}">checked</c:if> /></td>

			<td align="center"><input type="checkbox" name="usersListInGui"
				value="${user.id}" onchange=""
				<c:if test="${user.inGui}">checked</c:if>
				<c:if test="${user.role.name != 'ROLE_PAYEE'}">disabled</c:if> /></td>

			<td align="center"><input type="checkbox"
				name="usersListDeleted" value="${user.id}"
				<c:if test="${user.deleted}">checked</c:if> /></td>
			</tr>
		</c:forEach>
		<tr>
			<td align="right" colspan="4">
			<c:if test="${viewUserForm.pageNumber > 1}">
			<input  type="button" onclick="getPage(1)" value="<spring:message	code="${elements.suffix}.view.user.first" />" />
				<input  type="button" onclick="previous()" value="<spring:message	code="${elements.suffix}.view.user.previous.button" />" 	/>
			</c:if>
			</td>
			<td colspan="4" align="left">
			<c:if test="${viewUserForm.pageNumber < viewUserForm.pagesAvailable}">
				<input  type="button" onclick="next()" value="<spring:message code="${elements.suffix}.view.user.next.button" />" />
				<input  type="button" onclick="getPage(${viewUserForm.pagesAvailable})" value="<spring:message code="${elements.suffix}.view.user.last" />" />
				
			</c:if>
			</td>
		</tr>
		<tr>
			<td colspan="8" align="center">Page Size : <select id="pageSize"
				name="pageSize" onchange="getPageWithPageSize()">
				<option value="5">5</option>
				<option value="10">10</option>
				<option value="15">15</option>
				<option value="20">20</option>
				<option value="25">25</option>
				<option value="30">30</option>
				<option value="35">35</option>
			</select> <%
			try{
 	ViewUserForm viewUserForm = (ViewUserForm) request
 				.getAttribute("viewUserForm");
 		for (int i = 1; i <= viewUserForm.getPagesAvailable(); i++) {
 			if (i == viewUserForm.getPageNumber()) {
 %> <span> <%
 	out.print(i);
 %> </span> <%
 	} else {
 %> <span class="pager-page" onclick="getPage(<%=i%>)"> <%
 	out.print(i);
 %> </span> <%
 	}
 		}
			}catch(Exception e){
				
			}
 %>
			<div style="float: right;">view ${viewUserForm.pageNumber} of
			${viewUserForm.pagesAvailable}</div>
			</td>
		</tr>
		<tr>
			<td colspan="8" style="color: Green;"><spring:message
				code="${elements.suffix}.view.user.bottommessage" /><br> <form:errors
				path="*" cssClass="errors" />
			</td>
		</tr>
		<tr>
			<c:if test="${viewUserForm.totalRecords >= 0}">
				<td colspan="8"><input type="submit"
					class='ui-button ui-state-default ui-corner-all'
					id="updateUserList"
					value="<spring:message code="${elements.suffix}.view.user.update"/>"
					<c:if test="${viewUserForm.totalRecords == 0}">
                            disabled="disabled"
                        </c:if> /></td>
			</c:if>
		</tr>
	</table>
	<table width=98% class="formTable" cellspacing="0">
		<tr class="ui-state-default ui-th-column ui-th-ltr">
			<c:if test="${viewUserForm.pageNumber > 1}">
				<td align="left" colspan="10"><a onclick="previous()" href="#"
					class="mouseEffect"><span class="ui-icon ui-icon-seek-prev"
					style="float: left;"></span><spring:message
					code="${elements.suffix}.view.user.previous" /></a></td>
			</c:if>
			<c:if test="${viewUserForm.pageNumber < viewUserForm.pagesAvailable}">
				<td colspan="10" align="right"><a onclick="next()" href="#"
					class="mouseEffect"><span class="ui-icon ui-icon-seek-next"
					style="float: right;"></span><spring:message
					code="${elements.suffix}.view.user.next" /></a></td>
			</c:if>
		</tr>

	</table>
</form:form>

<script language="javascript" type="text/javascript">
	$(document).ready(function(){
		$("#pageSize").val($("#recordSize").val());
	});
    function next() {
        var pageNo = document.getElementById("pageNoId");
        var pageNoInt = parseInt(pageNo.value);
        pageNoInt += 1;
        pageNo.value = pageNoInt;
        document.getElementById("newSearch").value = "true";
        document.forms['searchForm'].submit();
    }

    function previous() {
        var pageNo = document.getElementById("pageNoId");
        var pageNoInt = parseInt(pageNo.value);
        pageNoInt -= 1;
        pageNo.value = pageNoInt;
        document.getElementById("newSearch").value = "true";
        document.forms['searchForm'].submit();
    }
    function clearFilters() {
        document.getElementById("firstNameId").value = "";
        document.getElementById("lastNameId").value = "";
        document.getElementById("roleId").value = "0";
        document.getElementById("emailId").value = "";
        document.getElementById("efinId").value = "";
        document.getElementById("ssnId").value = "";
        document.getElementById("cityId").value = "";
        document.getElementById("countryId").value = "";
        document.getElementById("zipId").value = "";
        document.getElementById("ptinId").value = "";
        document.getElementById("businessNameId").value = ""; 
        document.getElementById("refNumber").value = ""; 
        
    }
    function submitForm() {
        document.getElementById("pageNoId").value = 1;
        document.getElementById("newSearch").value = "true";
        document.forms['searchForm'].submit();
    }

    function getPage(page) {
		document.getElementById("pageNoId").value = page;
		document.forms['searchForm'].submit();		
	}

	function getPageWithPageSize(){
		document.getElementById("pageNoId").value =1;
		document.getElementById("recordSize").value = $("#pageSize").val();
		document.forms['searchForm'].submit();	
	}

    function sort(columnName) {
        document.getElementById("sortColumnId").value = columnName;
        var sortOrder = document.getElementById("sortOrderId").value;
        if (sortOrder == 'asc')
            document.getElementById("sortOrderId").value = 'desc';
        else
            document.getElementById("sortOrderId").value = 'asc';

        document.getElementById("newSearch").value = "true";
        document.forms['searchForm'].submit();
    }
</script>

<%@include file="/WEB-INF/jsp/include/footer.jsp"%>
</body>
</html>